package mosdi.tools;

import java.util.Arrays;

import mosdi.fa.Alphabet;
import mosdi.fa.IIDTextModel;
import mosdi.fa.TextModel;
import mosdi.matching.HorspoolMatcher;
import mosdi.matching.SundayMatcher;
import mosdi.util.Iupac;
import mosdi.util.Log;

public class HorspoolSimulation {

	public static void usage() {
		System.err.println("Usage: HorspoolSimulation <pattern> <textlength> <iterations>");
		System.exit(-1);
	}
	
	public static void main(String[] args) {
		if (args.length!=3) usage();
		String stringPattern = args[0];
		int textLength = Integer.parseInt(args[1]);
		int iterations = Integer.parseInt(args[2]);
		int maxCost = textLength*(stringPattern.length()+1); 
		Alphabet alphabet = Iupac.getDnaAlphabet();
		int[] pattern = alphabet.buildIndexArray(stringPattern);
		System.out.println("Analyzed pattern: "+stringPattern);
		System.out.println("Textlength: "+textLength);
		System.out.println("Textmodel: uniform i.i.d.");
		System.out.println("Iterations: "+iterations);
		int[] horspoolCostFrequency = new int[maxCost+1];
		int[] sundayCostFrequency = new int[maxCost+1];
		TextModel textModel = new IIDTextModel(alphabet.size());
		Log.setTimingActive(true);
		Log.startTimer();
		for (int i=0; i<iterations; ++i) {
			int[] text = textModel.generateRandomText(textLength);
			HorspoolMatcher.findMatches(alphabet.size(), text, pattern);
			horspoolCostFrequency[Math.min(HorspoolMatcher.comparisons,maxCost)]+=1;
			SundayMatcher.findMatches(alphabet.size(), text, pattern);
			sundayCostFrequency[Math.min(SundayMatcher.comparisons,maxCost)]+=1;
		}
		Log.stopTimer("Simulation");
		double[] horspoolCostDistribution = new double[maxCost+1];
		double[] sundayCostDistribution = new double[maxCost+1];
		for (int i=0; i<=maxCost; ++i) {
			horspoolCostDistribution[i]=((double)horspoolCostFrequency[i])/iterations;
			sundayCostDistribution[i]=((double)sundayCostFrequency[i])/iterations;
		}
		System.out.println("---------- Horspool -------------");
		System.out.println(Arrays.toString(horspoolCostDistribution));
		System.out.println("---------- Sunday -------------");
		System.out.println(Arrays.toString(sundayCostDistribution));
	}

}
